import axios from 'axios'

const state = {
	strategyList: [],
	strategyDetail: {
		info: {
			name: '',
			celuejieshao: ''
		}
	}
}

const mutations = {
	// 更改策略列表信息
	getStrategyList (state, payload) {
		state.strategyList = payload.res;
		console.log(state.strategyList)
	},
	// 更改策略详细信息
	getStrategyDetail (state, payload) {
		state.strategyDetail = payload.res;
		console.log(state.strategyDetail)
	},
	// 失败时清空strategyDetail
	emptyStrategyDetail (state) {
		state.strategyDetail = {
			info: {
				name: '',
				celuejieshao: ''
			}
		}
	}
	
}

const actions = {
	// 获取策略列表信息
	getStrategyList ({ commit }) {
		axios.get('http://api.fxnn.net/op')
		.then(function (res) {
			commit({
				type: 'getStrategyList',
				res: res.data.objs
			})
		})
		.catch(function (err) {
			console.log(err)
		})
	},

	// 获取策略详细信息
	getStrategyDetail ({commit}, param) {
		axios.get("http://api.fxnn.net/strategydetail?id="+ param.id +"&random="+ Math.random())
		.then(function (res) {
			commit({
				type: 'getStrategyDetail',
				res: res.data.data
			})
		})
		.catch(function (err) {
			console.log(err)
			commit({type: 'emptyStrategyDetail'})
		})
	}

}

export default {
	state,
	mutations,
	actions
}